package com.tinyshardingjdbc.tinyshardingjdbc.shardingasync;


import com.tinyshardingjdbc.tinyshardingjdbc.shardingasync.tool.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class PageUtils {


    public static List<String> getTableNameYm(String startDate, String enDate, String table) {
        List<String> months = DateUtils.calAllMonths(startDate, enDate);
        Long endMonth = Long.valueOf(enDate.replace("-", "").substring(0, 6));
        Long date = Long.valueOf(DateUtils.now().replace("-", "").substring(0, 6));
        if (endMonth > date) {
            return null;
        }
        List<String> tableList = new ArrayList<>();
        for (String m : months) {
            tableList.add(table + "_" + m);
        }
        return tableList;
    }

    public static List<String> getTableNameY(String startDate, String enDate, String table) {
        List<String> months = DateUtils.calAllMonths(startDate, enDate);
        Long endMonth = Long.valueOf(enDate.replace("-", "").substring(0, 4));
        Long date = Long.valueOf(DateUtils.now().replace("-", "").substring(0, 4));
        if (endMonth > date) {
            return null;
        }
        List<String> tableList = new ArrayList<>();
        for (String m : months) {
            tableList.add(table + "_" + m);
        }
        return tableList;
    }


    public static List<String> getTableNameHash(Integer count, String table) {
        List<String> tableList = new ArrayList<>();
        for (int i = 0; i <= count; i++) {
            tableList.add(table + "_" + i);
        }
        return tableList;
    }

    /**
     * 计算两个时间相差是否在3个月内
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 对比结果true是在3个月内，false为不在3个月内
     */
    public static Boolean getMonth(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(start);
        c2.setTime(end);
        c1.add(Calendar.MONTH, 3);
        if (c1.getTimeInMillis() <= c2.getTimeInMillis()) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


    public static void main(String[] args) {


    }

    public static Date date(String dateString) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

}
